//获取应用实例
const app = getApp()
var util = require("../../utils/util.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {   
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: false,
    baodan: true,
    logs: "",
    weizhi: "",
    times: ""
  },
  onLoad: function () {
    var that = this
    // 实例化腾讯地图API核心类
    qqmapsdk = new QQMapWX({
      key: 'L6FBZ-YQ4CG-URYQY-IH36I-SXRFZ-IEBI5' //此处使用你自己申请的key
    })
    var that = this;
    //允许登录
    if (app.globalData.userInfo) {
      // console.log(app.globalData.userInfo)
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        // console.log(res.userInfo)
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          // console.log(res.userInfo)
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    //获取信息
    that.setData({
      logs: (wx.getStorageSync('logs') || []).map(function (log) {
        return util.formatTime(new Date(log))
      })
    })
  
    wx.request({
      url: "https://myzhongchou.net/public/index.php/api/index/getcarousel",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
         console.log(res)
         console.log(wx.getStorageSync("session3rd"))
        that.setData({
          imgUrls: res.data.msg
        })
      }
    })
  },
  baodan: function () {
    var that = this;
    that.setData({
      baodan: false,
      times: util.formatTime(new Date())
    })
    wx.getLocation({
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        // 调用接口
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            that.setData({
              weizhi: res.result.address
            })
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    })

  },
  submit: function (e) {
    var that = this;
    var session3rd = wx.getStorageSync("session3rd");
    // 用户判断登录信息
    wx.request({
      url: "https://myzhongchou.net/public/index.php/api/index/status",
      data:{
        session3rd: session3rd
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        if (res.data.success==0){
          wx.showToast({
            title: res.data.msg,
            duration:2000         
          })  


          var submit = e.detail.value;
          var timestamp = Date.parse(new Date()) / 1000;
          if (submit.position == "") {
            wx.showToast({
              title: '请填写所在位置',
              image: '../../img/gantan.png'
            })
            return false;
          }
          if (that.data.times == "") {
            wx.showToast({
              title: '请填写到店时间',
              image: '../../img/gantan.png'
            })
            return false;
          }
          wx.request({
            url: "https://myzhongchou.net/public/index.php/api/index/insertorder",
            methods: "post",
            data: {
              address: submit.position,
              time: that.data.times,
              date: timestamp,
              session3rd: session3rd
            }, header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res);
              if (res.data.success == 0) {
                wx.showToast({
                  title: '成功',
                  icon: 'success'
                })
                that.setData({
                  baodan: true
                })
              } else {
                wx.showToast({
                  title: '网络错误，请稍后重试',
                  image: '../../img/gantan.png'
                })
              }
            }
          })


       } else if (res.data.error == 1){
         
        } else if (res.data.error == 2){
         
        } else if (res.data.error == 3){
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
        
      }
    })


    
  },
  onShow:function(){
    var that=this;
    wx.request({
      url: "https://myzhongchou.net/public/index.php/api/index/getcarousel",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res.data.msg)
        that.setData({
          imgUrls: res.data.msg
        })
      }
    })

  }
})
